package com.zm.zmgz.service;

import com.zm.zmgz.dao.SalarySettlementDetailsBeanMapper;
import com.zm.zmgz.pojo.*;
import com.zm.zmgz.utils.CollectionUtilszm;
import com.zm.zmgz.utils.StringCjwUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/03/25/15:12
 * @Description:工资结算明细表服务
 */
@Service
public class SalarySettlementDetailsService {

    @Autowired
    SalarySettlementService salarySettlementService;

    @Autowired
    SalarySettlementDetailsBeanMapper salarySettlementDetailsBeanMapper;

    @Autowired
    EmployeeService employeeService;


    /***
     * @Description: 根据批次和状态（已发放 or 未发放），获取SalarySettlementDetailsBean集合
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-03-26 19:10:33
     */
    public List<SalarySettlementDetailsBean> getSalarySettlementDetailsByBatchAndStatus(int batch,String status){

        SalarySettlementDetailsBeanExample salarySettlementDetailsBeanExample = new SalarySettlementDetailsBeanExample();
        SalarySettlementDetailsBeanExample.Criteria criteria = salarySettlementDetailsBeanExample.createCriteria();
        criteria.andBatchEqualTo(batch).andStatusEqualTo(status);
        List<SalarySettlementDetailsBean> salarySettlementDetailsBeanList = salarySettlementDetailsBeanMapper.selectByExample(salarySettlementDetailsBeanExample);
        if(!Objects.isNull(salarySettlementDetailsBeanList) && salarySettlementDetailsBeanList.size()>0) {
            System.out.println("第"+batch+"批次"+status+"工资的员工："+salarySettlementDetailsBeanList);
            return salarySettlementDetailsBeanList;
        } else {
            System.out.println("第"+batch+"批次没有"+status+"的员工！");
            return null;
        }
    }



    /***
     * @Description: 查看批次内工资发放情况，返回： "未发放"|"部分发放"|"全部发放"
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020年3月26日 16点08分
     */
    public String getDistributionSituationByBatchId(int batch) {
        List<SalarySettlementDetailsBean> salarySettlementDetailsBeanList = getSalarySettlementDetailsByBatchAndStatus(batch, "未发放");
        if(!Objects.isNull(salarySettlementDetailsBeanList) && salarySettlementDetailsBeanList.size()>0) { // 未发放 or 部分发放
            List<SalarySettlementDetailsBean> salarySettlementDetailsBeanList2 = getSalarySettlementDetailsByBatchAndStatus(batch, "已发放");
            if(!Objects.isNull(salarySettlementDetailsBeanList2) && salarySettlementDetailsBeanList2.size()>0) {
                return "部分发放";
            } else {
                return "未发放";
            }
        } else {
            return "全部发放";
        }
    }

    public int addSalarySettlementDetails(List<SalarySettlementDetailsBean> salarySettlementDetailsBeanList) {
        int i = 0;
        for (SalarySettlementDetailsBean salarySettlementDetailsBean : salarySettlementDetailsBeanList) {
            int insert = salarySettlementDetailsBeanMapper.insert(salarySettlementDetailsBean);
            i += insert;
        }
        System.out.println("工资结算明细表添加了"+i+"条记录");
        return i;
    }

    /***
    * @Description: 获取最近工资结算明细记录集合,id为空=最新批次 并用status字段排序;设置name,statusBoolean,index,selected属性
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-03-25 15:33:06
    */
    public List<SalarySettlementDetailsBean> getSalarySettlementDetailsByBatch(Integer id,int page,int limit) {

        int total = 0;
        if(Objects.isNull(id)) {
            SalarySettlementBean newestSalarySettlement = salarySettlementService.getNewestSalarySettlement();
            if(!Objects.isNull(newestSalarySettlement)) {
                id = newestSalarySettlement.getId();
            } else {
                System.out.println("暂无批次明细信息！");
                return null;
            }
        }
        SalarySettlementDetailsBeanExample salarySettlementDetailsBeanExample = new SalarySettlementDetailsBeanExample();
        SalarySettlementDetailsBeanExample.Criteria criteria = salarySettlementDetailsBeanExample.createCriteria();
        criteria.andBatchEqualTo(id);
//        salarySettlementDetailsBeanExample.setOrderByClause("status desc");
        List<SalarySettlementDetailsBean> salarySettlementDetailsBeanList = salarySettlementDetailsBeanMapper.selectByExample(salarySettlementDetailsBeanExample);
        if(Objects.isNull(salarySettlementDetailsBeanList) || salarySettlementDetailsBeanList.size() == 0) {
            return null;
        }
        total = salarySettlementDetailsBeanList.size();
        salarySettlementDetailsBeanList = (List<SalarySettlementDetailsBean>)CollectionUtilszm.getListPagination(salarySettlementDetailsBeanList, page, limit);
        System.out.println("第"+id+"批次的记录条数:"+salarySettlementDetailsBeanList.size());
        if(salarySettlementDetailsBeanList.size()>0){
            // 设置name,statusBoolean,index,selected属性
            int i = 1;
            for (SalarySettlementDetailsBean salarySettlementDetailsBean : salarySettlementDetailsBeanList) {
                salarySettlementDetailsBean.setIndex(i++); // 设置index
                String identificationnumber = salarySettlementDetailsBean.getIdentificationnumber();
                String name = employeeService.getNameByIdentificationnumber(identificationnumber);
                EmployeeBean employeeBean = employeeService.getEmployeeByIdentificationnumber(identificationnumber);

                String employeeid = employeeBean.getEmployeeid();
                if(!Objects.isNull(employeeid)) {
                    salarySettlementDetailsBean.setEmployeeId(employeeBean.getEmployeeid()); // 临时添加员工编号
                }
                String status1 = employeeBean.getStatus(); // 临时添加员工状态
                if(status1.equals("0")){
                    salarySettlementDetailsBean.setEmployeeStatus("试用");
                } else if(status1.equals("1")){
                    salarySettlementDetailsBean.setEmployeeStatus("转正");
                } else{
                    salarySettlementDetailsBean.setEmployeeStatus("离职");
                }
                salarySettlementDetailsBean.setName(name); // 临时添加姓名
                String status = salarySettlementDetailsBean.getStatus();
                if(status.equals("已发放")){ // 设置statusBoolean
                    salarySettlementDetailsBean.setStatusBoolean(true);
                } else {
                    salarySettlementDetailsBean.setStatusBoolean(false);
                }
                salarySettlementDetailsBean.setSelected(salarySettlementDetailsBean.getStatusBoolean()); // 设置selected
            }

            int i2 = 0;
            salarySettlementDetailsBeanList.sort(new Comparator<SalarySettlementDetailsBean>() {
                @Override
                public int compare(SalarySettlementDetailsBean o1, SalarySettlementDetailsBean o2) {
                    if(Objects.isNull(o1.getProjectname()) || o1.getProjectname().length() == 0 || o1.getProjectname().equals("")) {
                        return -1;
                    }
                    if(!o1.getProjectname().equals(o2.getProjectname())) {
                        return StringCjwUtils.isMoreThan(o1.getProjectname(),o2.getProjectname());
                    } else {
                        return StringCjwUtils.isMoreThan(o1.getIdentificationnumber(),o2.getIdentificationnumber());
                    }
                }
            });
            salarySettlementDetailsBeanList.get(0).setTotal(total); // 排好序后,在赋值有多少条数据
            return salarySettlementDetailsBeanList;
        } else {
            System.out.println("暂无批次明细信息！");
            return null;
        }
    }


    /***
    * @Description: 更新SalarySettlementDetailsBean
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-03-27 01:37:30
    */
    public int updateSalarySettlementDetail(SalarySettlementDetailsBean salarySettlementDetail) {
//        if(salarySettlementDetail.getStatusBoolean()) {
//            salarySettlementDetail.setStatus("已发放");
//        } else {
//            salarySettlementDetail.setStatus("未发放");
//        }
        System.out.println("salarySettlementDetail.remark:"+salarySettlementDetail.getRemark());
        int i = salarySettlementDetailsBeanMapper.updateByPrimaryKey(salarySettlementDetail);
        return i;
    }

    /***
    * @Description: 该批次是否全部发放完毕
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-03-27 19:14:02
    */
    public Boolean getStatusAll(Integer id) {
        if(Objects.isNull(id)) {
            System.out.println("批次为空,获取最新批次!");
            SalarySettlementBean newestSalarySettlement = salarySettlementService.getNewestSalarySettlement();
            if(Objects.isNull(newestSalarySettlement)){
                System.out.println("暂无批次明细信息!");
                return false;
            }
            System.out.println("最新批次为:"+newestSalarySettlement.getId());
            id = newestSalarySettlement.getId();
        }
        List<SalarySettlementDetailsBean> salarySettlementDetailsBeanList = getSalarySettlementDetailsByBatchAndStatus(id, "未发放");
        if(!Objects.isNull(salarySettlementDetailsBeanList) && salarySettlementDetailsBeanList.size() > 0) {
            System.out.println("第"+id+"批次未全部发放!");
            return false;
        } else {
            System.out.println("第"+id+"批次全部发放!");
            return true;
        }

    }

    /***
    * @Description: 该批次全部发放或暂缓
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-03-27 19:54:48
    */
    public Boolean allIssueOrHoldOff(AllIssueOrHoldOffBean allIssueOrHoldOffBean) {
        // dq
        SalarySettlementDetailsBeanExample salarySettlementDetailsBeanExample = new SalarySettlementDetailsBeanExample();
        SalarySettlementDetailsBeanExample.Criteria criteria = salarySettlementDetailsBeanExample.createCriteria();
        criteria.andBatchEqualTo(allIssueOrHoldOffBean.getId());
        List<SalarySettlementDetailsBean> salarySettlementDetailsBeanList = salarySettlementDetailsBeanMapper.selectByExample(salarySettlementDetailsBeanExample);
        if(!Objects.isNull(salarySettlementDetailsBeanList) && salarySettlementDetailsBeanList.size()>0) {
            String status = "";
            if(allIssueOrHoldOffBean.getStatusAll()) {
                status = "已发放";
                for (SalarySettlementDetailsBean salarySettlementDetailsBean : salarySettlementDetailsBeanList) {
                    salarySettlementDetailsBean.setStatus(status);
                    salarySettlementDetailsBean.setIssueuser(allIssueOrHoldOffBean.getName());
                    salarySettlementDetailsBean.setIssuedate(allIssueOrHoldOffBean.getDate());
                    salarySettlementDetailsBeanMapper.updateByPrimaryKey(salarySettlementDetailsBean);
                }
            } else {
                status = "未发放";
                for (SalarySettlementDetailsBean salarySettlementDetailsBean : salarySettlementDetailsBeanList) {
                    salarySettlementDetailsBean.setStatus(status);
                    salarySettlementDetailsBean.setIssueuser(null);
                    salarySettlementDetailsBean.setIssuedate(null);
                    salarySettlementDetailsBeanMapper.updateByPrimaryKey(salarySettlementDetailsBean);
                }
            }

            return true;
        } else {
            return false;
        }
    }

    /***
    * @Description: 删除一个SalarySettlementDetailsBean
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-03-28 10:12:28
    */
    public int handleDelete(SalarySettlementDetailsBean row) {
        return salarySettlementDetailsBeanMapper.deleteByPrimaryKey(row.getId());
    }

    /***
    * @Description: 把传入的结算明细状态取反;
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-03-30 16:31:45
    */
    public int updateSingleStatus(SalarySettlementDetailsBean salarySettlementDetailsBean,String name) {
        if(!salarySettlementDetailsBean.getStatusBoolean()) {
            salarySettlementDetailsBean.setIssuedate(null);
            salarySettlementDetailsBean.setIssueuser(null);
            salarySettlementDetailsBean.setStatus("未发放");
        } else {
            salarySettlementDetailsBean.setIssuedate(new Date());
            salarySettlementDetailsBean.setIssueuser(name);
            salarySettlementDetailsBean.setStatus("已发放");
        }
        int i = salarySettlementDetailsBeanMapper.updateByPrimaryKey(salarySettlementDetailsBean);
        return i;
    }

    /***
    * @Description: 添加一个salarySettlementDetailsBean(收支明细对象)
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-03-31 07:38:51
    */
    public int addSalarySettlementDetail(SalarySettlementDetailsBean salarySettlementDetailsBean) {
        salarySettlementDetailsBean.setStatus("未发放");
        List<SalarySettlementDetailsBean> salarySettlementDetailsBeanList = getSalarySettlementDetailsByBatch(salarySettlementDetailsBean.getBatch(), 1, 1); // 判断当前批次是否有批次明细记录,没有批次内id最大值就是0
        int maxBatchId = 0;
        if (!Objects.isNull(salarySettlementDetailsBeanList) && salarySettlementDetailsBeanList.size() > 0) {
            maxBatchId = salarySettlementDetailsBeanMapper.getMaxBatchIdByBatch(salarySettlementDetailsBean.getBatch());
        }
        System.out.println("当前"+salarySettlementDetailsBean.getBatch()+"批次内最大的批次id为:"+maxBatchId);
        salarySettlementDetailsBean.setBatchid(maxBatchId+1);
        return salarySettlementDetailsBeanMapper.insert(salarySettlementDetailsBean);
    }

    /***
    * @Description: 获取新增批次明细模板数据
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-04-22 17:45:30
    */
    public SalarySettlementDetailsBean getDetailTemplate(String identificationnumber) {
        SalarySettlementDetailsBean salarySettlementDetailsBean = new SalarySettlementDetailsBean();
        EmployeeBean employeeBean = employeeService.getEmployeeByIdentificationnumber(identificationnumber);
        salarySettlementDetailsBean.setName(employeeBean.getName());
        salarySettlementDetailsBean.setEmployeeId(employeeBean.getEmployeeid());
        salarySettlementDetailsBean.setIdentificationnumber(employeeBean.getIdentificationnumber());
        salarySettlementDetailsBean.setProjectname(employeeBean.getProjectname());
        String status1 = employeeBean.getStatus(); // 临时添加员工状态
        if(status1.equals("0")){
            salarySettlementDetailsBean.setEmployeeStatus("试用");
        } else if(status1.equals("1")){
            salarySettlementDetailsBean.setEmployeeStatus("转正");
        } else{
            salarySettlementDetailsBean.setEmployeeStatus("离职");
        }
        salarySettlementDetailsBean.setCoefficient("21.75"); // 设置系数

        BigDecimal trialsalary = null;
        if(employeeBean.getStatus().equals("0")){
            trialsalary = employeeBean.getTrialsalary();
        } else {
            trialsalary = employeeBean.getSalary();
        }
        if(!Objects.isNull(trialsalary)) {
            trialsalary = trialsalary.multiply(new BigDecimal(1000));
            salarySettlementDetailsBean.setBasicwage(trialsalary.toString()); // 设置基本工资
            salarySettlementDetailsBean.setDayrate(trialsalary.divide(new BigDecimal(21.75), 2)); // 设置日均工资,保留两位小数,有余数末尾加1
        }
        salarySettlementDetailsBean.setNeeddays(new BigDecimal(21)); // 设置应出勤天数
        salarySettlementDetailsBean.setTruedays(new BigDecimal(21)); // 设置实际出勤天数

        return salarySettlementDetailsBean;
    }

    /***
    * @Description: 删除指定批次的所有批次明细
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-04-22 21:35:17
    */
    public int deleteByBatch(int batch) {
        SalarySettlementDetailsBeanExample salarySettlementDetailsBeanExample = new SalarySettlementDetailsBeanExample();
        SalarySettlementDetailsBeanExample.Criteria criteria = salarySettlementDetailsBeanExample.createCriteria();
        criteria.andBatchEqualTo(batch);
        return salarySettlementDetailsBeanMapper.deleteByExample(salarySettlementDetailsBeanExample);

    }

    /***
    * @Description: 根据批次和身份证删除一条批次明细记录
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-04-23 20:32:01
    */
    public int deleteByBatchAndIdentificationnumber(SalarySettlementDetailsBean bean) {
        SalarySettlementDetailsBeanExample salarySettlementDetailsBeanExample = new SalarySettlementDetailsBeanExample();
        SalarySettlementDetailsBeanExample.Criteria criteria = salarySettlementDetailsBeanExample.createCriteria();
        criteria.andBatchEqualTo(bean.getBatch()).andIdentificationnumberEqualTo(bean.getIdentificationnumber());
        return salarySettlementDetailsBeanMapper.deleteByExample(salarySettlementDetailsBeanExample);


    }

    /***
    * @Description: 通过批次id获取详细记录
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-05-21 18:39:17
    */
    public List<SalarySettlementDetailsBean> getSalarySettlementDetailsBeanByBatchId(Integer integer) {
        SalarySettlementDetailsBeanExample salarySettlementDetailsBeanExample = new SalarySettlementDetailsBeanExample();
        SalarySettlementDetailsBeanExample.Criteria criteria = salarySettlementDetailsBeanExample.createCriteria();
        criteria.andBatchEqualTo(integer);
        List<SalarySettlementDetailsBean> salarySettlementDetailsBeanList = salarySettlementDetailsBeanMapper.selectByExample(salarySettlementDetailsBeanExample);
        return salarySettlementDetailsBeanList;
    }

    /***
    * @Description: 通过批次id和状态值获取详细记录
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-05-21 20:07:01
    */
    public List<SalarySettlementDetailsBean> getSalarySettlementDetailsBeanByBatchIdAndstatus(Integer integer, String status) {
        SalarySettlementDetailsBeanExample salarySettlementDetailsBeanExample = new SalarySettlementDetailsBeanExample();
        SalarySettlementDetailsBeanExample.Criteria criteria = salarySettlementDetailsBeanExample.createCriteria();
        criteria.andBatchEqualTo(integer).andStatusEqualTo(status);
        List<SalarySettlementDetailsBean> salarySettlementDetailsBeanList = salarySettlementDetailsBeanMapper.selectByExample(salarySettlementDetailsBeanExample);
        return salarySettlementDetailsBeanList;
    }
}
